package cn.com.dealme.modules;

import javax.servlet.http.HttpServletRequest;

import org.nutz.ioc.annotation.InjectName;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.View;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Param;

import cn.com.dealme.Keys;
import cn.com.dealme.domain.model.User;
import cn.com.dealme.nutz.view.DJsonErrMapView;
import cn.com.dealme.nutz.view.DJsonView;
import cn.com.dealme.service.UserService;
import cn.com.dealme.service.exception.UserServiceException;

@InjectName("UserModule")
@At("/user")
public class UserModule {
	
	private static final Log log = Logs.getLog(UserModule.class);

	private UserService userService;
	
	private HttpServletRequest req;
	
	

	@At("/login")
	public View login(	@Param("account") String account,
						@Param("password") String password,
						@Param("checkCode") String checkCode ) {
		
		//取验证码   // 验证码判断
		Object correctCheckCode = req.getSession().getAttribute(Keys.CheckCode);
		if(null == correctCheckCode || !correctCheckCode.equals(checkCode)) {
			if(null == correctCheckCode) {
				if(log.isErrorEnabled()) {
					log.error("corrent checkcode is null !!");
				}
			}
			//报告验证码错误
			return new DJsonErrMapView(UserServiceException.CheckCodeError);
		}
		try {
			//登录成功
			User user = userService.managerLogin(account, password);
			req.getSession().setAttribute(Keys.Manager, user.getId());
			
		} catch (UserServiceException e) {
			//用户操作错误提示
			return new DJsonErrMapView(e);
		}
		//返回成功视图
		return DJsonView.createExtSuccessView();
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public HttpServletRequest getReq() {
		return req;
	}

	public void setReq(HttpServletRequest req) {
		this.req = req;
	}

}
